#!/usr/bin/python
# -*- coding: UTF-8 -*-
import requests
import json
import pandas as pd
import pymongo
from  config import *
client = pymongo.MongoClient(MONGO_URI)
db = client["zhihu"]

follows_url = 'https://www.zhihu.com/api/v4/members/{user}/followees?include={include}&offset={offset}&limit={limit}'
user_url = 'https://www.zhihu.com/api/v4/members/{user}?include={include}'
start_user = 'cai-tong'
user_query = 'locations,employments,gender,educations,business,voteup_count,thanked_Count,follower_count,following_count,cover_url,following_topic_count,following_question_count,following_favlists_count,following_columns_count,answer_count,articles_count,pins_count,question_count,commercial_question_count,favorite_count,favorited_count,logs_count,marked_answers_count,marked_answers_text,message_thread_token,account_status,is_active,is_force_renamed,is_bind_sina,sina_weibo_url,sina_weibo_name,show_sina_weibo,is_blocking,is_blocked,is_following,is_followed,mutual_followees_count,vote_to_count,vote_from_count,thank_to_count,thank_from_count,thanked_count,description,hosted_live_count,participated_live_count,allow_message,industry_category,org_name,org_homepage,badge[?(type=best_answerer)].topics'
follows_query = 'data[*].answer_count,articles_count,gender,follower_count,is_followed,is_following,badge[?(type=best_answerer)].topics'
followers_query = 'data[*].answer_count,articles_count,gender,follower_count,is_followed,is_following,badge[?(type=best_answerer)].topics'

headers={
    "authorization": "oauth c3cef7c66a1843f8b3a9e6a1e3160e20",
    "user-agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
}


def get_all_users(page):
    print(page)
    url=follows_url.format(user=start_user, include=follows_query, limit=20, offset=page)
    try:
        res=requests.get(url,headers=headers)
        if res.status_code==200:
            result=res.text
            re=json.loads(result)
            with open("test.txt","w",encoding="utf-8") as f:
                f.write(result)
            print(re)
        else:
            print(res.status_code)
    except Exception as e:
        print(e)
def get_data():
    queryArgs = {}
    projectionFields = {'_id': False}  # 用字典指定
    result = db["users"].find(queryArgs,projectionFields)
    return result

def main():
    data=pd.DataFrame(list(get_data()))
    data.to_csv("caitong_follwers.csv",encoding="utf-8",index=False)


if __name__ == '__main__':
    main()